﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="MvcSystem.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：MvcSystem.cs
// 创建时间：2014-12-15 21:38
// 修改时间：2014-12-15 21:38
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using WebCommon.SysConfig;

namespace WebCommon.SysConfig
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using WebCommon.IO;
    /// <summary>
    /// SYS配置
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class  Config<T> where T : class, new()
    {
        private readonly string[] shuoming =
            {
                "*配置文件注意事项",
                "*星号(*)开头的是注释：不能修改",
                "*没有星号(*)开头的，等号(=)左边为字段名：不能修改" ,
                "*等号(=)右边为属性值：可以修改",
                "*------------------------------------------"
            };

        private readonly string path;


        public  Config(string path)
        {
            this.path = path;

        }

        /// <summary>
        /// 如果没有文件，则初始化该文件
        /// </summary>
        /// <returns></returns>
        public bool Initialization()
        {
            if (!File.Exists(this.path))
            {
                FileOperation.CreateDirectory(this.path);
                this.Save(new T());
                return true;
            }

            return false;
        }

        /// <summary>
        /// 当前目录生成说明文件
        /// </summary>
        public void SaveInterpretation()
        {
            var file = new System.IO.FileInfo(this.path);
            var directory = file.Directory;
            File.WriteAllLines(directory + "/配置文件说明.txt", this.shuoming);
        }

        /// <summary>
        /// 保存配置文件
        /// </summary>
        public void Save(T t)
        {
            FileOperation.CreateDirectory(path);
            var type = typeof(T);
            var propertyInfos = type.GetProperties();
            var list = new List<string>();

            for (int i = 0; i < propertyInfos.Length; i++)
            {
                var customAttributes = propertyInfos[i].GetCustomAttributes(false);
                if (customAttributes.Length > 0)
                {
                    var zhushiAttribute = customAttributes[0] as ConfigInterpretationAttribute;
                    if (zhushiAttribute != null)
                    {
                        list.Add("*" + zhushiAttribute.Name);
                    }
                }

                list.Add(propertyInfos[i].Name + "=" + propertyInfos[i].GetValue(t, null));
                list.Add(string.Empty);
            }
            File.WriteAllLines(this.path, list);
        }

        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <returns></returns>
        public T Model
        {
            get
            {
                var readAllLines = File.ReadAllLines(path).Where(x => !(x.StartsWith("*") || string.IsNullOrEmpty(x)));

                var nt = new T();

                var type = typeof(T);
                var propertyInfos = type.GetProperties();
                foreach (var strings in readAllLines.Select(item => item.Split(new[] { '=' }, 2)))
                {
                    if (strings.Length != 2)
                    {
                        continue;
                    }
                    foreach (var prop in propertyInfos.Where(prop => prop.Name == strings[0]))
                    {
                        prop.SetValue(nt, strings[1], null);
                    }
                }
                return nt;
            }
        }
    }
}